<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/09/25 14:09
 * Desc: 特产应用接口
 */
class Controller_Pc_Api_V2_Techan extends Controller_Pc_Api_v2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Techan
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Techan
     */
    private $service;


    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('techan', $this->request_params);

        // 服务
        $this->service = new Service_Api_Techan();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 特产搜索条件
    public function action_get_query_condition()
    {
        try {
            $this->result['data'] = $this->service->query_condition();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 列表页搜索
    public function action_get_list()
    {
        try {
            $this->validation->get_list()->check();

            $this->result['data'] = $this->service->search($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取特产产品详情
    public function action_get_detail()
    {
        try {
            $this->validation->get_detail()->check();

            $this->result['data'] = $this->service->info($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 订单创建
    public function action_create_order()
    {
        try {
            $this->validation->create_order()->check();

            $this->request_params['member_info'] = $this->member_info;
            $this->result['data']                = $this->service->create_order($this->request_params);
        } catch (Exception $err) {
            $this->result['status'] = false;
            $this->result['msg']    = $err->getMessage();
            St_Log::write('特产订单创建', array($err->getTraceAsString()), 'debug/log');
        }
    }

    // 套餐列表
    public function action_suit_list()
    {
        try {
            $this->validation->suit_list()->check();

            $this->result['data'] = $this->service->suit_list($this->request_params, $this->mid);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 首页热门
    public function action_index_recommend()
    {
        try {
            $this->validation->index_recommend()->check();

            $this->result['data']['list'] = $this->service->recommend($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取快递费用
    public function action_get_express_fee()
    {
        try {
            $this->validation->get_express_fee()->check();

            $this->result['data'] = $this->service->get_express_fee($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }
}